/*
 * LoginJFrame.java
 *
 * Created on 19 kwiecień 2008, 17:36
 */
package schoolmanagement;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.WindowConstants;
import schoolmanagement.controller.*;

/**
 *
 * @author  deely
 */
public class LoginJFrame extends javax.swing.JFrame {

    static private LoginJFrame instance;

    /** Creates new form LoginJFrame */
    public LoginJFrame() {
        initComponents();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        DBAccess.Connect();

        addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                DBAccess.Dispose();
            }
        });
    }

    static public void setInstance(LoginJFrame inst) {
        instance = inst;
    }

    static public LoginJFrame getInstance() {
        return instance;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpMainPanel = new javax.swing.JPanel();
        jtbLogin = new javax.swing.JTextField();
        jpassField = new javax.swing.JPasswordField();
        jlbUsername = new javax.swing.JLabel();
        jlbPassword = new javax.swing.JLabel();
        jbtLogin = new javax.swing.JButton();
        jlbErrorMsg = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("LoginForm"); // NOI18N
        setResizable(false);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(schoolmanagement.SchoolmanagementApp.class).getContext().getResourceMap(LoginJFrame.class);
        jpMainPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanelLogin.border.title"))); // NOI18N
        jpMainPanel.setName("jPanelLogin"); // NOI18N

        jtbLogin.setText(resourceMap.getString("jTextUser.text")); // NOI18N
        jtbLogin.setName("jTextUser"); // NOI18N

        jpassField.setText(resourceMap.getString("jPassword.text")); // NOI18N
        jpassField.setName("jPassword"); // NOI18N
        jpassField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jpassFieldKeyPressed(evt);
            }
        });

        jlbUsername.setText(resourceMap.getString("jLabelUser.text")); // NOI18N
        jlbUsername.setName("jLabelUser"); // NOI18N

        jlbPassword.setText(resourceMap.getString("jlbPassword.text")); // NOI18N
        jlbPassword.setName("jlbPassword"); // NOI18N

        javax.swing.GroupLayout jpMainPanelLayout = new javax.swing.GroupLayout(jpMainPanel);
        jpMainPanel.setLayout(jpMainPanelLayout);
        jpMainPanelLayout.setHorizontalGroup(
            jpMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpMainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jlbUsername)
                    .addComponent(jlbPassword))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jpassField, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtbLogin, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpMainPanelLayout.setVerticalGroup(
            jpMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpMainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtbLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlbUsername))
                .addGap(18, 18, 18)
                .addGroup(jpMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jpassField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlbPassword))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(schoolmanagement.SchoolmanagementApp.class).getContext().getActionMap(LoginJFrame.class, this);
        jbtLogin.setAction(actionMap.get("OnLogin")); // NOI18N
        jbtLogin.setText("Zaloguj"); // NOI18N
        jbtLogin.setName("jBtnLogin"); // NOI18N
        jbtLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtLoginActionPerformed(evt);
            }
        });

        jlbErrorMsg.setFont(resourceMap.getFont("jlbErrorMsg.font")); // NOI18N
        jlbErrorMsg.setForeground(resourceMap.getColor("jlbErrorMsg.foreground")); // NOI18N
        jlbErrorMsg.setText(resourceMap.getString("jlbErrorMsg.text")); // NOI18N
        jlbErrorMsg.setName("jlbErrorMsg"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jpMainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jlbErrorMsg)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpMainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(jlbErrorMsg))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jbtLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtLoginActionPerformed
        this.jlbErrorMsg.setText(""); // clear error label
        char[] pwd = jpassField.getPassword();
        String strPwd = new String();

        for (int j = 0; j < pwd.length; ++j) {
            strPwd += pwd[j];
        }

        if (User.Login(jtbLogin.getText(), strPwd)) {
            MainJFrame mv;
            if ((mv = MainJFrame.getInstance()) != null) {
                mv.setVisible(true);
            } else {
                mv = new MainJFrame();//SchoolmanagementApp.getApplication());
                mv.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
                mv.addWindowListener(new WindowAdapter() {

                    @Override
                    public void windowClosing(WindowEvent e) {
                        User.Logout();
                        LoginJFrame.getInstance().setVisible(true);
                    }
                });
                SchoolmanagementApp.getApplication().show(mv);
            }
            this.jpassField.setText("");
            this.setVisible(false);
        } else {
            jlbErrorMsg.setText("Invalid username or password!");
        }
}//GEN-LAST:event_jbtLoginActionPerformed

    private void jpassFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jpassFieldKeyPressed
        int key = evt.getKeyCode();
        
        if (key == KeyEvent.VK_ENTER) {
            jbtLogin.getActionListeners()[0].actionPerformed(new java.awt.event.ActionEvent(jbtLogin, ActionEvent.ACTION_PERFORMED, ""));

        }
    }//GEN-LAST:event_jpassFieldKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LoginJFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbtLogin;
    private javax.swing.JLabel jlbErrorMsg;
    private javax.swing.JLabel jlbPassword;
    private javax.swing.JLabel jlbUsername;
    private javax.swing.JPanel jpMainPanel;
    private javax.swing.JPasswordField jpassField;
    private javax.swing.JTextField jtbLogin;
    // End of variables declaration//GEN-END:variables
}
